﻿using EasyNetQ;
using EasyNetQ.Scheduling;
using EasyNetQTest.Message;
using System;

namespace EasyNetQTest.PublisherDelayed
{
    /// <summary>
    /// 发布订阅topic模式,延时发布
    /// 消息发布者
    /// </summary>
    class Program
    {
        static void Main(string[] args)
        {
            //var connStr = "host=192.168.80.71;virtualHost=EDCVHOST;username=admin;password=edison";
            var connStr = "host=192.168.0.248;username=admin;password=admin;publisherConfirms=true";
            int msgCout = 10;
            string msg = "延时发送的消息";

            //连接mq
            //使用RabbitMQ延迟消息插件
            using (var bus = RabbitHutch.CreateBus(connStr, x => x.Register<IScheduler, DelayedExchangeScheduler>()))
            {

                //循环发送消息
                for (int i = 0; i < msgCout; i++)
                {
                    var message = msg + i;
                    var msgModel = new MyMessage
                    {
                        MsgId = i,
                        MsgContent = message
                    };

                    //Publish模式，需要先进行一次订阅
                    //在订阅时会创建订阅的队列。然后再发布消息时才会发到订阅的队列中

                    //消息发布,使用交换机topic方式发布，队列名为Test.TestQueue*
                    //延时发布,时间为UTC时间
                    //这里是延时10秒发送消息
                    bus.FuturePublish(DateTime.UtcNow.AddSeconds(10), msgModel);

                    Console.WriteLine(" 发布消息: {0}", message);
                }

                Console.WriteLine("\n发布结束\n输入 [enter] 退出程序.\n");
                //程序不结束，等待输入
                Console.ReadLine();
            }
        }
    }
}